#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2023/10/30 11:09
# @Author  : zhaowp1109
# @File    : __init__.py.py
# @Description :
import time

from sqlalchemy import Column, Integer,DateTime,func,String,text,Boolean
from sqlalchemy.ext.declarative import as_declarative, declared_attr
from snowflake import SnowflakeGenerator
from sqlalchemy import event
from sqlalchemy.schema import UniqueConstraint

def generator_snowflake_id(length=32):
    '''uuid ID'''
    gen = SnowflakeGenerator(length)
    return next(gen)

@as_declarative()
class Base:
    # id = Column(Integer,primary_key=True, index=True)
    id = Column(Integer,primary_key=True, index=True)
    create_at = Column(DateTime, server_default=func.now(), comment='创建时间')
    update_at = Column(DateTime, server_default=func.now(), server_onupdate=func.now(), comment='创建时间')
    delete_time = Column(Integer,server_default="0",default=0,comment="删除标记")
    __name__: str
    # Generate __tablename__ automatically
    @declared_attr
    def __tablename__(cls) -> str:
        return cls.__name__.lower()

    def receive_change_event(self, verb, key, value, oldvalue):
        print('---------------------------------------------')
        s = "Value '%s' %s on attribute '%s', " % (value, verb, key)
        if oldvalue:
            s += "which replaced the value '%s', " % oldvalue
        s += "on object %s" % self
        print(s)